package com.md.controller;

import com.md.api.authentication.UsersApi;
import com.md.application.service.users.UsersApplicationService;
import com.md.client.login.LoginClient;
import com.md.model.request.IdRequest;
import com.md.model.request.authentication.users.AddUsersRequest;
import com.md.model.request.authentication.users.LoginRequest;
import com.md.model.request.authentication.users.RegisterUsersRequest;
import com.md.model.request.authentication.users.UpdateUsersRequest;
import com.md.model.request.login.AzureSamlLoginRequest;
import com.md.model.response.authentication.users.RegisterUsersResponse;
import com.md.model.response.authentication.users.UsersLoginResponse;
import com.md.model.response.authentication.users.UsersResponse;
import com.md.model.system.CommonResult;
import lombok.RequiredArgsConstructor;
import org.dom4j.DocumentException;
import org.springframework.web.bind.annotation.RestController;

import java.net.MalformedURLException;
import java.net.URISyntaxException;

/**
 * @author 刘骄阳
 * 2022-04-01 18:28
 */
@RestController
@RequiredArgsConstructor
public class UsersController implements UsersApi {
  private final UsersApplicationService usersApplicationService;
  private final LoginClient loginClient;

  /**
   * 添加用户
   *
   * @param request 用户请求实体
   * @return 操作结果
   */
  @Override
  public CommonResult<Long> insert(final AddUsersRequest request) {
    return usersApplicationService.insert(request);
  }

  @Override
  public CommonResult<Long> responses() throws MalformedURLException, URISyntaxException, DocumentException {
    return usersApplicationService.responses();
  }

  @Override
  public CommonResult<String> demo() {

    String samlRequestXmlString = null;
    try {
      // 初始化链接
//      InitializationService.initialize();

//      samlRequestXmlString = SamlUtils.createRequestXmlString("https://login.microsoftonline.com/00618cc4-0e79-43ef-9df6-41ddc9a38d62/saml2",
//          "https://3s35r49356.oicp.vip/users/response", "a2c39ba2-3cc0-4a44-acaa-20b96dc35ebd");
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    loginClient.azureSaml(new AzureSamlLoginRequest());
    return null;
  }

  /**
   * 注册
   *
   * @param request 用户请求实体
   * @return 操作结果
   */
  @Override
  public CommonResult<RegisterUsersResponse> register(final RegisterUsersRequest request) {
    return usersApplicationService.register(request);
  }

  /**
   * 登录
   *
   * @param request 用户请求实体
   * @return 操作结果
   */
  @Override
  public CommonResult<UsersLoginResponse> login(final LoginRequest request) {
    return usersApplicationService.login(request);
  }

  /**
   * 修改用户信息
   *
   * @param request 用户请求实体
   * @return 操作结果
   */
  @Override
  public CommonResult<Void> updateOther(final UpdateUsersRequest request) {
    return usersApplicationService.updateOther(request);
  }

  /**
   * 修改当前用户
   *
   * @param request 用户请求实体
   * @return 操作结果
   */
  @Override
  public CommonResult<Void> updateSelf(final UpdateUsersRequest request) {
    return usersApplicationService.updateSelf(request);
  }

  /**
   * 查询用户信息
   *
   * @param request 用户请求实体
   * @return 操作结果
   */
  @Override
  public CommonResult<UsersResponse> queryUserInfo(final IdRequest request) {
    return usersApplicationService.queryUserInfo(request);
  }
}
